home *** CD-ROM | disk | FTP | other *** search
- Path: news.magic.mb.ca!not-for-mail
- From: wrightd@merlin.magic.mb.ca (David C. Wright)
- Newsgroups: comp.lang.c
- Subject: Re: ugly constants and header files
- Date: 6 Jan 1996 23:55:01 -0600
- Organization: Magic Online Services Winnipeg.
- Message-ID: <k607w0zpkP3U088yn@merlin.magic.mb.ca>
- References: <1996Jan3.155754.111142@kuhub.cc.ukans.edu>
- Reply-To: wrightd@merlin.magic.mb.ca
- NNTP-Posting-Host: merlin.magic.mb.ca
- X-Newsreader: Yarn 0.88 with YES 0.20
- X-Info: YES is the Yarn Editor Shell, a freeware utility for Yarn!
- The best supplement to the best offline newsreader!
-
- In group comp.lang.c, article <1996Jan3.155754.111142@kuhub.cc.ukans.edu>,
- anh@kuhub.cc.ukans.edu telepathically conveyed:
- >Hello,
- >
- >I have a list of constants, some defined using #define, some are string
- >constants a la char *gSuperStrings[]={"aa","bb"};
- >
- >These constants are used by multiple modules which are linked together to
- >a program. However, I have more than one program using these constants,
- >hence I put the constants in a header file. I know it is ugly to put
- >definitions in a header file.
- >
- >My current solution is to change all the #define to integer constants and
- >to keep 2 files:
- >
- >my_const.h --- To be included only once (perferably by the main()
- > module). Definitions
- >
- >my_ext.h --- To be included by any modules using the constants.
- > Declarations.
- >
- >Is this the best solution? Are there any other way?
-
- Create a single header file, of the form:
-
- [begin file]
- /* whatever */
-
- #define CONSTANT 320
-
- struct anystruct
- {
- int onevar;
- int twovar;
- };
- typedef struct anystruct anystruct_t;
-
- enum enumvars = {one, two, three};
- typedef enum enumvars enumvars_t;
-
- #ifdef GLOBALS
-
- int globalvar;
- anystruct_t *ptr1;
- enumvars_t evars;
-
- #else
-
- extern int globalvar;
- extern anystruct_t *ptr1;
- extern enumvars_t evars;
-
- #endif
-
- int anyfunction(void);
-
- /* whatever */
- [end file]
-
- #include this file in any modules that use the #defines/variables/prototypes
- in question. In *one* of the modules, use;
-
- #define GLOBALS
-
- *before* the #include statement that includes your header file. The net
- effect is that the variables/structures/whatever will be declared once in
- the module that has the #define statement in it, and, in all the others, it
- will be declared as an external, which will satisfy the linker. All the
- definitions, such as structures, typedefs, #defines, prototypes, etc will
- be defined in every module. Everything will be happy.
-
- --
- David C. Wright - wrightd@merlin.magic.mb.ca
- E-mail me for my PGP public key. My public key changed on Dec 5, 1995.
-
- And God said: E = *mv* - Ze*/r ...and there *WAS* light!
-